<?php
namespace app\admin\controller;

use think\Db;

class Message extends Base
{
    /**
     * 留言列表
     * @return mixed|\think\response\Json
     */
    public function index()
    {
        if(request()->isAjax()){

            $param = input('param.');

            $limit = $param['pageSize'];
            $offset = ($param['pageNumber'] - 1) * $limit;

            $where = ['is_delete' => 0];
            if (!empty($param['searchText'])) {
                $where['name|email|phone'] = ['like', '%' . $param['searchText'] . '%'];
            }

            $selectResult = Db::name('message')->where($where)->limit($offset, $limit)->order('id desc')->select();

            foreach($selectResult as $key=>$vo){
                $selectResult[$key]['add_time'] = date('Y-m-d H:i:s', $vo['add_time']);
                $selectResult[$key]['operate'] = showOperate($this->makeButton($vo['id']));
            }

            $return['total'] = Db::name('message')->where($where)->count();  // 总数据
            $return['rows'] = $selectResult;

            return json($return);
        }

        return $this->fetch();
    }

    public function messageDel()
    {
        $id = input('param.id');

        try{
            Db::name('message')->where('id',$id)->update(['is_delete' => 1]);
            return apiReturn(1, '删除成功!');
        }catch (\Exception $e){
            return apiReturn(-1,$e->getMessage().$e->getLine());
        }
    }

    /**
     * 拼装操作按钮
     * @param $id
     * @return array
     */
    private function makeButton($id)
    {
        return [
            '删除' => [
                'auth' => 'message/messageedel',
                'href' => "javascript:articleDel(" . $id . ")",
                'btnStyle' => 'danger',
                'icon' => 'fa fa-trash-o'
            ]
        ];
    }
}
